#include "stm32f4xx_tim.h"
#include "stm32f4xx_flash.h"
#include "iap.h"
#include "CAN.h"
#define  MAIN_DEF_SET 1
#include "mainInclude.h"

extern   u16           udstime,udstime1;
extern   u32	       uds_crc,uds_can_crc;
extern   unsigned char SciBootFlag;
extern   SciLoadInfo   SciLoadInfoUse;
extern   CanLoadInfo   CanLoadInfoUse;

extern   void NVIC_Configuration(void);
extern   void OSTimerInit(void);
extern   void Debug_Serial_Init(u32 baud);
extern   void NVIC_Configuration_Rset(void);
extern   int16_t WriteBootPara(uint16 value);
extern   void CAN_vInit(uint32 CanBaudRate);
extern   void CAN_BusoffError(void);
extern   void iap_load_app(uint32_t appxaddr);
extern   void CanUpLoadMain(void);
extern   void GetUniqueSN(void);
extern   uint8_t CAN_vGetMsgObj(void);
extern   void IniLedIo(void);

static uint16   StartTime = 0;
uint32 tt32;
//int main(void)
//{ 
//	ReadDevInfo(); // 得到当前舵机的ID号	
//	GetUniqueSN();

//	//Debug_Serial_Init(DevInfoCur.SciBaudRate);
//	IniLedIo();
//	CAN_vInit(DevInfoCur.CanBaudRate);
//	OSTimerInit();
//	Delay_Init();
//	//InitEeprom();
//	NVIC_Configuration();
//	//IWDG_Configuration();	

//	StartTime = 0;

//	while (1)
//    {
//  		while( (StartTime < 2000) || (DevInfoCur.AppUpgradeFlag == 1) )  
//  		{
//  			//喂独立看门狗
//  			//IWDG_ReloadCounter();
//  			
//  			//串口升级
//  			//UartUpLoadMain();

//			//CAN升级
//			CanUpLoadMain();
//  		}
//  
//  		//运行app
//  		//IWDG_ReloadCounter();
//  		NVIC_Configuration_Rset();
//  		//__disable_irq();
//  		iap_load_app(APP_START_ADDRESS); 
//  		//tt32 = (*(vu32*)APP_START_ADDRESS);
//    }
//}

/***********************************************************************
*	void TIM6_IRQHandler(void)
    0.1ms定时器
***********************************************************************/
void TIM6_DAC_IRQHandler(void)
{
	static u16 sharkledtimer=0;

	if(TIM_GetITStatus(TIM6,TIM_IT_Update) != RESET)
	{
		TIM_ClearITPendingBit(TIM6,TIM_FLAG_Update);

		CAN_BusoffError();

		if(StartTime++ >= 2000)
			{StartTime = 2000;}

		if(1 == SciLoadInfoUse.LoadStep)
		{
          if(SciLoadInfoUse.LoadResetCounter++ >= 3*10000)
          	{
          	   SciLoadInfoUse.LoadResetCounter = 3*10000;
               SciLoadInfoUse.LoadResetCounter = 0;
			   SciLoadInfoUse.LoadStep = 0;
		    }
		}

		if(CanLoadInfoUse.LoadResetCounter++ >= 3*10000)
        {
            CanLoadInfoUse.LoadResetCounter = 0;
			CanLoadInfoUse.RxSum = 0;
			CanLoadInfoUse.LoadStep = 0;
			CanLoadInfoUse.LoadPadeCounter = 0;
			CanLoadInfoUse.ReciveLen = 0;
			uds_can_crc = 0xffffffff;
		}

		//can报文接收
		 CAN_vGetMsgObj();

		//PC报文接收报文处理
        // PCUSART_IRQ0p1ms();

		sharkledtimer++;
		if(sharkledtimer <= 5000)
		{
			GPIO_SetBits(GPIOE, GPIO_Pin_2);
		}
		else if(sharkledtimer > 5000 && sharkledtimer < 10000)
		{
			GPIO_ResetBits(GPIOE, GPIO_Pin_2);
		}
		else if(sharkledtimer >= 10000)
		{
			sharkledtimer = 0;
		}
	}
}	





